<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico">
  <link rel="mask-icon" href="/images/favicon.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"remove","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":"false;","trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>
<meta name="description" content="1. IP协议 IP协议是无连接的通信协议，他不会占用两个正在通信的计算机之间的通信线路，这样IP就降低了对网络线路的需求，每条线可以同时满足许多不同的计算机之间的通信需要。 通过IP消息或者其他数据会被分割为较小的独立的包，并通过因特网在计算机之间传送。 IP负责将每个包路由至他的目的地，但IP协议没有确认数据包是否按顺序发送或者包是否损坏，所以IP数据包是不可靠的，需要由他的上层协议作出控制。">
<meta property="og:type" content="article">
<meta property="og:title" content="TCP的三次握手">
<meta property="og:url" content="http://example.com/2021/11/02/TCP%E7%9A%84%E4%B8%89%E6%AC%A1%E6%8F%A1%E6%89%8B/index.html">
<meta property="og:site_name" content="ADGai&#39;s Blog">
<meta property="og:description" content="1. IP协议 IP协议是无连接的通信协议，他不会占用两个正在通信的计算机之间的通信线路，这样IP就降低了对网络线路的需求，每条线可以同时满足许多不同的计算机之间的通信需要。 通过IP消息或者其他数据会被分割为较小的独立的包，并通过因特网在计算机之间传送。 IP负责将每个包路由至他的目的地，但IP协议没有确认数据包是否按顺序发送或者包是否损坏，所以IP数据包是不可靠的，需要由他的上层协议作出控制。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/AD-Gai-Code/pic-go/raw/master/202110221650641.png">
<meta property="og:image" content="https://gitee.com/AD-Gai-Code/pic-go/raw/master/202110221656279.png">
<meta property="article:published_time" content="2021-11-02T00:43:59.164Z">
<meta property="article:modified_time" content="2022-03-01T13:21:06.254Z">
<meta property="article:author" content="AiLaodu">
<meta property="article:tag" content="计算机网">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/AD-Gai-Code/pic-go/raw/master/202110221650641.png">


<link rel="canonical" href="http://example.com/2021/11/02/TCP%E7%9A%84%E4%B8%89%E6%AC%A1%E6%8F%A1%E6%89%8B/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2021/11/02/TCP%E7%9A%84%E4%B8%89%E6%AC%A1%E6%8F%A1%E6%89%8B/","path":"2021/11/02/TCP的三次握手/","title":"TCP的三次握手"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>TCP的三次握手 | ADGai's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">ADGai's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container"></div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container">
  <div class="algolia-stats"><hr></div>
  <div class="algolia-hits"></div>
  <div class="algolia-pagination"></div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/11/02/TCP%E7%9A%84%E4%B8%89%E6%AC%A1%E6%8F%A1%E6%89%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="AiLaodu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="ADGai's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          TCP的三次握手
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-11-02 08:43:59" itemprop="dateCreated datePublished" datetime="2021-11-02T08:43:59+08:00">2021-11-02</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-03-01 21:21:06" itemprop="dateModified" datetime="2022-03-01T21:21:06+08:00">2022-03-01</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91/" itemprop="url" rel="index"><span itemprop="name">计算机网</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="1-IP协议"><a href="#1-IP协议" class="headerlink" title="1. IP协议"></a>1. IP协议</h2><ul>
<li><code>IP</code>协议是无连接的通信协议，他不会占用两个正在通信的计算机之间的通信线路，这样<code>IP</code>就降低了对网络线路的需求，每条线可以同时满足许多不同的计算机之间的通信需要。</li>
<li>通过<code>IP</code>消息或者其他数据会被分割为较小的独立的包，并通过因特网在计算机之间传送。</li>
<li><code>IP</code>负责将每个包路由至他的目的地，但<code>IP</code>协议没有确认数据包是否按顺序发送或者包是否损坏，所以<code>IP</code>数据包是不可靠的，需要由他的上层协议作出控制。</li>
</ul>
<span id="more"></span>

<h2 id="2-TCP-三次握手的过程"><a href="#2-TCP-三次握手的过程" class="headerlink" title="2. TCP 三次握手的过程"></a>2. TCP 三次握手的过程</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">A：您好，我是 A。</span><br><span class="line">B：您好 A，我是 B。</span><br><span class="line">A：您好 B。</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/AD-Gai-Code/pic-go/raw/master/202110221650641.png" alt="TCP三次握手"></p>
<p><img src="https://gitee.com/AD-Gai-Code/pic-go/raw/master/202110221656279.png" alt="img"></p>
<ul>
<li>首先 B 处于 <code>LISTEN（监听）</code>状态，等待客户的连接请求。</li>
<li>A 向 B 发送连接请求报文，<code>SYN=1，ACK=0</code>，选择一个初始的序号 x。</li>
<li>B 收到连接请求报文，如果同意建立连接，则向 A 发送连接确认报文，<code>SYN=1，ACK=1</code>，确认号为 <code>x+1</code>，同时也选择一个初始的序号 <code>y</code>。</li>
<li>A 收到 B 的连接确认报文后，还要向 B 发出确认，确认号为 <code>y+1</code>，序号为 <code>x+1</code>。</li>
<li>B 收到 A 的确认后，连接建立。</li>
</ul>
<h2 id="3-为什么要三次握手"><a href="#3-为什么要三次握手" class="headerlink" title="3. 为什么要三次握手"></a>3. 为什么要三次握手</h2><p><strong>三次握手的目的是建立可靠的通信信道，说到通讯，简单来说就是数据的发送与接收，而三次握手最主要的目的就是双方确认自己与对方的发送与接收是正常的。</strong></p>
<p>第一次握手：Client 什么都不能确认；Server 确认了对方发送正常，自己接收正常</p>
<p>第二次握手：Client 确认了：自己发送、接收正常，对方发送、接收正常；Server 确认了：对方发送正常，自己接收正常</p>
<p>第三次握手：Client 确认了：自己发送、接收正常，对方发送、接收正常；Server 确认了：自己发送、接收正常，对方发送、接收正常</p>
<p>所以三次握手就能确认双发收发功能都正常，缺一不可。</p>
<p>当然 A 发给 B 的应答之应答也会丢，也会绕路，甚至 B 挂了。按理来说，还应该有个应答之应答之应答，这样下去就没底了。所以四次握手是可以的，四十次都可以，关键四百次也不能保证就真的可靠了。只要双方的消息都有去有回，就基本可以了。</p>
<p>其次，第三次握手是为了防止失效的连接请求到达服务器，让服务器错误打开连接。</p>
<p>客户端发送的连接请求如果在网络中滞留，那么就会隔很长一段时间才能收到服务器端发回的连接确认。客户端等待一个超时重传时间之后，就会重新请求连接。但是这个滞留的连接请求最后还是会到达服务器，如果不进行三次握手，那么服务器就会打开两个连接。如果有第三次握手，客户端会忽略服务器之后发送的对滞留连接请求的连接确认，不进行第三次握手，因此就不会再次打开连接。</p>
<h3 id="3-1-第-2-次握手传回了-ACK，为什么还要传回-SYN？"><a href="#3-1-第-2-次握手传回了-ACK，为什么还要传回-SYN？" class="headerlink" title="3.1 第 2 次握手传回了 ACK，为什么还要传回 SYN？"></a>3.1 第 2 次握手传回了 ACK，为什么还要传回 SYN？</h3><p>接收端传回发送端所发送的 <code>ACK</code> 是为了告诉客户端，我接收到的信息确实就是你所发送的信号了，这表明从客户端到服务端的通信是正常的。而回传 <code>SYN</code> 则是为了建立并确认从服务端到客户端的通信。”</p>
<blockquote>
<p><code>SYN</code> 同步序列编号(Synchronize Sequence Numbers) 是 <code>TCP/IP</code> 建立连接时使用的握手信号。在客户机和服务器之间建立正常的 <code>TCP</code> 网络连接时，客户机首先发出一个 <code>SYN</code> 消息，服务器使用 <code>SYN-ACK</code> 应答表示接收到了这个消息，最后客户机再以 <code>ACK(Acknowledgement）</code>消息响应。这样在客户机和服务器之间才能建立起可靠的 <code>TCP</code> 连接，数据才可以在客户机和服务器之间传递。</p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91/" rel="tag"># 计算机网</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/09/04/%E9%80%92%E5%BD%92%E7%AE%97%E6%B3%95%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="prev" title="递归算法学习笔记">
                  <i class="fa fa-chevron-left"></i> 递归算法学习笔记
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/11/02/TCP%E7%9A%84%E5%9B%9B%E6%AC%A1%E6%8C%A5%E6%89%8B/" rel="next" title="TCP的四次挥手">
                  TCP的四次挥手 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">AiLaodu</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="https://cdn.jsdelivr.net/npm/algoliasearch@4.10.5/dist/algoliasearch-lite.umd.js" integrity="sha256-HWlivbjXc58GuU4EIZziqIE83FFZ/da42de13pGZnMA=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4.30.2/dist/instantsearch.production.min.js" integrity="sha256-eqhx/eer5fsD7YooSb21wsfJaQkJ/4gF3bmRBZP7q2o=" crossorigin="anonymous"></script><script src="/js/third-party/search/algolia-search.js"></script>





  





</body>
</html>
